//
//  ShareManager.m
//  KidReading
//
//  Created by YangShuai on 16/5/24.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ShareManager.h"
#import "ConstDefine.h"
@implementation ShareManager
static ShareManager* singleShareManager = nil;
+(ShareManager*)shareManager{
    if (singleShareManager == nil) {
        @synchronized (self) {
            singleShareManager = [[ShareManager alloc] init];
        }
    }
    return singleShareManager;
}
//分享方法
-(void)JRShareWithText:(NSString*)text ImageArray:(NSArray*)imageArray Url:(NSURL*)shareUrl Title:(NSString*)title Type:(SSDKContentType)type BaseView:(UIView*)baseView statusChange:(void (^)(SSDKResponseState state, SSDKPlatformType type))statuChange{
    
//    [DataProcessing do_Main_quick:^{
//        NSArray * platforms =@[@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeQQ), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeFacebook)];
//        if ([self hasWhatsApp]) {
//            SSUIShareActionSheetCustomItem* item_whatApp = [SSUIShareActionSheetCustomItem itemWithIcon:[UIImage imageNamed:@"sns_icon_whatapp.png"] label:NSLocalizedString(@"whatsapp", "") onClick:^{
//                //
//                [self whatsAppSendText:[shareUrl absoluteString]];
//            }];
//            
//            platforms =@[@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeQQ), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeFacebook),item_whatApp];
//        }
//        
//        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
//        [shareParams SSDKSetupShareParamsByText:text
//                                         images:imageArray
//                                            url:shareUrl
//                                          title:title
//                                           type:type];
//        //单独设置新浪微博分享方式
//        [shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"%@ %@",text,shareUrl] title:title image:imageArray url:shareUrl latitude:0 longitude:0 objectID:nil type:SSDKContentTypeAuto];
//        [shareParams SSDKSetupFacebookParamsByText:[NSString stringWithFormat:@"%@ %@",text,shareUrl] image:imageArray type:SSDKContentTypeAuto];
//        //2、分享（可以弹出我们的分享菜单和编辑界面）
//        SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:baseView //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
//                                                                         items:platforms
//                                                                   shareParams:shareParams
//                                                           onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
//                                                               if (statuChange) {
//                                                                   statuChange(state,platformType);
//                                                               }
//                                                               switch (state) {
//                                                                   case SSDKResponseStateSuccess:
//                                                                   {
//                                                                       //
//                                                                       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"talkSharesuccess","")
//                                                                                                                           message:nil
//                                                                                                                          delegate:nil
//                                                                                                                 cancelButtonTitle:NSLocalizedString(@"sure", "")
//                                                                                                                 otherButtonTitles:nil];
//                                                                       [alertView show];
//                                                                       break;
//                                                                   }
//                                                                   case SSDKResponseStateFail:
//                                                                   {
//                                                                       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"talkSharefail","")
//                                                                                                                       message:nil
//                                                                                                                    delegate:nil
//                                                                                                             cancelButtonTitle:NSLocalizedString(@"sure", "")
//                                                                                                             otherButtonTitles:nil, nil];
//                                                                       [alert show];
//                                                                       break;
//                                                                   }
//                                                                   default:
//                                                                       break;
//                                                               }
//                                                           }
//                                                 ];
//        [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
//        [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeFacebook)];
//        
//    }];
}

-(BOOL)hasWhatsApp {
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]];
}

-(void)whatsAppSendText:(NSString*)message {
    //分享文字
//    NSString *urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",@"https://a.app.qq.com/o/simple.jsp"];
//    NSString *urlEncoded = [urlWhats stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSURL *whatsappURL = [NSURL URLWithString:urlEncoded];
//    if ([self hasWhatsApp]) {
//        [[UIApplication sharedApplication] openURL:whatsappURL];
//    }
    
    //分享链接
    NSString* str = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                          (CFStringRef)message,
                                                                                          NULL,
                                                                                          CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                                          kCFStringEncodingUTF8));
    
    NSString* text = [NSString stringWithFormat:@"whatsapp://send?text=%@",str];
    NSURL *whatsappURL = [NSURL URLWithString:text];
    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    }
    
    
}

-(void)dealloc{};

@end
